cap log close

global path = "****"

local name= "household_spillovers"
local date = "052720"
global logname = "$path/`name'_`date'.log"

clear
set more off
set matsize 11000
cd "$path"
log using "$logname", replace

use "$path/Data/penalty_sample.dta", clear

replace age_2017=0 if age_2017<0

gen age_45_64 = (age_2017 >= 45 & age_2017 <= 64)

*drop single person households

bysort tin: gen person_id = _n
bysort tin: egen hh_size = max(person_id)
drop if hh_size == 1

*2016 coverage for 45-64 yr olds
gen middle_aged_all16 = (age_45_64 == 1 & notall16 == 0)
gen middle_aged_notall16 = (age_45_64 == 1 & notall16 == 1)

*indicator for whether househould has individual of any age with < 11 months of 2016 cov
bysort tin: egen notall16_hh = max(notall16)

*indicator for whether household has middle aged individual with < 11 months of 2016 cov
bysort tin: egen middle_aged_notall16_hh = max(middle_aged_notall16)

*indicator for whether household has middle aged individual with 11 months of 2016 cov
bysort tin: egen middle_aged_all16_hh = max(middle_aged_all16)

estimates clear
eststo clear

*Col 1
reg covered1718 treatment if notall16_hh == 1 & notall16 == 0 & age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)
estimates save "spillovers_`date'.ster", replace 

*Col 2
reg covered1718 treatment if middle_aged_all16_hh == 1 & notall16 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)
estimates save "spillovers_`date'.ster", append

*Col 3
reg dead1718 treatment if notall16_hh == 1 & notall16 == 0 & age_45_64 == 1, cl(tin)
eststo
estadd scalar nobs e(N)
summ `e(depvar)' if e(sample)&treatment==0
estadd scalar ymean=r(mean)*100
estimates save "spillovers_`date'.ster", append

log close


